#!/usr/bin/env python
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# NOTE: Not tests for this file as it will be replaced with standard
# version that includes tests.


"""Google App Engine Python path configuration."""

import optparse
import os
import platform
import sys

from google.appengine import dist
from google.appengine.dist import _library


LIB_PATHS = [
    'antlr3',
    'fancy_urllib',
    'graphy',
    'ipaddr',
    'simplejson',
    'webob',
    ('yaml', 'lib'),
]


def set_paths(appengine_home):
  """Configure path list with App Engine dependency libraries.

  Adds all relevent App Engine libaries to path.

  Args:
    appengine_home: App Engine home containing App Engine and dependencies.
  """
  try:
    import django
  except ImportError:
    dist.use_library('django', '1.2')

  for path in LIB_PATHS:
    if isinstance(path, basestring):
      path = [path]
    sys.path.append(os.path.join(appengine_home, 'lib', *path))


def set_options(parser):
  """Configure an OptionParser to have standard app engine path options.

  Args:
    parser: OptionParser instance.
  """
  django_package = _library.PACKAGES['django']
  django_versions = django_package[1].keys()
  parser.add_option('--django', choices=django_versions, dest='django',
                    help='Use Django VERSION.  Defaults to 1.2.',
                    metavar='VERSION')


def options_parser():
  """Create a new OptionParser with standard app engine path options.

  Returns:
    OptionParser instance.
  """
  parser = optparse.OptionParser('usage: %prog [options]')
  set_options(parser)
  return parser


def main(argv, output=None, appengine_home=None):
  """Main program used for printing platform specific shell environment.

  Args:
    argv: Command line arguments as passed in to sys.argv.
    output: Output stream to write environment information to.  Defaults to
      stdout.
  """
  parser = options_parser()
  options, args = parser.parse_args(argv)

  if len(args) > 1:
    parser.error('No parameters accepted')

  if output is None:
    output = sys.stdout

  if appengine_home is None:
    this_directory = os.path.dirname(__file__)
    appengine_home = os.path.join(this_directory, os.pardir, os.pardir, os.pardir)
    appengine_home = os.path.normpath(appengine_home)
    appengine_home = appengine_home.replace('"', '\\"')
    
  set_paths(appengine_home)

  python_path = os.pathsep.join(sys.path)

  if platform.platform() == 'Windows':
    format = 'SET "PYTHONPATH=%s"\n'
  else:
    format = 'export "PYTHONPATH=%s"\n'

  output.write(format % python_path)
